package 
{
	import assets.UIAsset;
	import common.event.DUIEventer;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.SharedObject;
	import interfaces.IDUIHomeDemo;
	import managers.DDataManager;
	import managers.MapManager;
	import util.MapDataUtil;
	import vo.MOConfigVO;

	import vo.LocationVO;
	import vo.MapObjectVO;
	
	/**
	 * ...
	 * @author lgyu
	 */
	public class DHomeManager extends Sprite 
	{
		private var so:SharedObject = SharedObject.getLocal("homeDemo")
		private static const ADDLISTENER:String = "addListener"
		private var m_ui:IDUIHomeDemo = null
		private var _mapInited:Boolean = false;
		private var editModuleStatic:int = 0; //  编辑模式  交换物品
		private var _dataManager:DDataManager = new DDataManager
		
		private var _mapData:Object
		private var _bgId:int
		private var _curSelectMO:MapObjectVO
		private var _moList:Array = []
		
		
		
		public function DHomeManager():void 
		{
		}
		
		public function init(ui:IDUIHomeDemo = null, dnet:*=null):void 
		{
			
			
			_dataManager.so = so
			_bgId = _dataManager.getMapBGId()
			
			_moList =  _dataManager.getMOList()
			
			initMapData()
			
			m_ui = ui
			
			initUIEvent(ADDLISTENER)
			
			m_ui.initBg(_bgId)
			m_ui.setMapObjects(_moList)
		}
		
		private function initMapData():void 
		{
			var mo:MapObjectVO = new MapObjectVO 
			mo.typeId = _dataManager.getMapBGId()
			_mapData = MapDataUtil.getMapGrid(mo,false)
			
			for (var i:int = 0; i < _moList.length; i++ ) {
				var cmo:MapObjectVO = MapObjectVO.toMVO(_moList[i] as Object)
				addMOToMapData(cmo)
			}
		}
		
		
		private function addMOToMapData(mo:MapObjectVO):void 
		{
			var moData:Object = MapDataUtil.getMapGrid(mo)
			MapDataUtil.showData(moData)
			
			for ( var i:String in moData ) {
				_mapData[i] = 1
			}
		}
		
		private function removeMOFromMapData(mo:MapObjectVO):void 
		{
			var moData:Object = MapDataUtil.getMapGrid(mo)
			MapDataUtil.showData(moData)
			
			for ( var i:String in moData ) {
				_mapData[i] = 0
			}
		}
		
		
		
		
		private function initUIEvent(addlistener:String):void 
		{
			//DUIEventer[ADDLISTENER](DUIEventer.MAP_INITED, onMapInited)
			DUIEventer[ADDLISTENER](DUIEventer.GET_MAP_OBJECTS, onGetMapObjects)
			DUIEventer[ADDLISTENER](DUIEventer.EDIT_MODULE_START, onEditModuleStart)
			DUIEventer[ADDLISTENER](DUIEventer.EDIT_MODULE_NOSAVE_END, onEditModuleSave)
			DUIEventer[ADDLISTENER](DUIEventer.EDIT_MODULE_SAVE_END, onEditModuleSave)
			DUIEventer[ADDLISTENER](DUIEventer.TRY_SELECT_MAP_OBJECT, onSelectMO)
			
			DUIEventer[ADDLISTENER](DUIEventer.TRY_MOVE_MAP_OBJECT_TO, onMoveMOTo)
			DUIEventer[ADDLISTENER](DUIEventer.TRY_PLACE_MAP_OBJECT_AT, onPlaceMOAt)
			DUIEventer[ADDLISTENER](DUIEventer.RECYCLE_MAP_OBJECT, onRecycleMO)
			DUIEventer[ADDLISTENER](DUIEventer.TRY_CHANGE_BG, onChangeBg)
			DUIEventer[ADDLISTENER](DUIEventer.ROTATE_MAP_OBJ, onRotateMO)
			DUIEventer[ADDLISTENER](DUIEventer.TRY_SWITCH_POSITIONS, onSwitchPositions)
			
			
		}
		
		private function onSwitchPositions(mo1:MapObjectVO, mo2:MapObjectVO):void 
		{
			m_ui.switchPosition(true)
		}
		
		private function onRotateMO(mo:MapObjectVO):void 
		{
			
		}
		
		private function onChangeBg(bgId:int):void 
		{
			m_ui.changeBG(new MapObjectVO)
		}
		
		private function onRecycleMO(mo:MapObjectVO):void 
		{
			m_ui.recycle(true)
		}
		
		private function onPlaceMOAt(loc:LocationVO):void 
		{
			//trace("location:" + loc)
			_curSelectMO.positionX = loc.x
			_curSelectMO.positionY = loc.y
			var result:Boolean = tryAddMoToMapData(_curSelectMO)
			if (result) {
				addMOToMapData(_curSelectMO)
				if (_moList.indexOf(_curSelectMO) != -1) {
					_moList.push(_curSelectMO);
				}
				_curSelectMO = null
			}
			m_ui.putMoTo(result)
		}
		
		private function onMoveMOTo(loc:LocationVO):void 
		{
			_curSelectMO.positionX = loc.x
			_curSelectMO.positionY = loc.y
			
			var result:Boolean =  tryAddMoToMapData(_curSelectMO)
			
			m_ui.move(result)
			
		}
		
		private function tryAddMoToMapData(mo:MapObjectVO):Boolean
		{
			var result:Boolean = true
			var moData:Object = MapDataUtil.getMapGrid(mo)
			for (var i:String in moData) {
				if (_mapData[i] == 1 || _mapData[i] == null) {
					result = false
					break;
				}
 			}
			return result
		}
		
		private function getMObyItemId(itemId:int):MapObjectVO
		{
			var mo:MapObjectVO
			var hasRequestMo:Boolean = false
			for (var i:int = 0; i < _moList.length ; i++ ) {
				mo = _moList[i] as MapObjectVO
				if (mo) {
					if (mo.itemId == itemId) {
						hasRequestMo = true
						break;
					}
				}
			}
			
			if (hasRequestMo) return mo
			else throw new Error("地图上没有发现该id的物品")
		}
		
		private function onSelectMO(moItemId:int):void 
		{
			//todo: 检查 mo 是否存在
			_curSelectMO = getMObyItemId(moItemId);
			removeMOFromMapData(_curSelectMO)
			
			
		}
		
		private function onEditModuleStart():void 
		{
			/*
			editModuleStatic = 0 // default
			editModuleStatic = 1 // edit
			editModuleStatic = 2 // exit without save 
			editModuleStatic = 3 // save and exit  */
			//switch( editModuleStatic)
			//{
				//case 1: {
					editModuleStatic = 1;
					//break;
				//}
				//case 2: {
					//editModuleStatic = 0
					//undoEdit()
					//break;
				//}
				//case 3: {
					//editModuleStatic = 0
					//saveEdit()
					//break;
				//}
			//}
		}
		private function onEditModuleSave():void {
			editModuleStatic = 3;
		}
		
		private function saveEdit():void 
		{
			
		}
		
		private function undoEdit():void 
		{
			
		}
		
		private function onMapInited():void 
		{
			//trace("onMapInited")
			_mapInited = true; 
			var moList:Array =  _dataManager.getMOList()
			m_ui.setMapObjects(moList)
		
		}
		
		private function onGetMapObjects():void 
		{
			var moList:Array =  _dataManager.getMOList()
			m_ui.setMapObjects(moList)
		}
		
		
		
		
	}
	
}